package com.eatme.web.app.article.controller;

import com.eatme.web.app.article.entity.Article;
import com.eatme.web.app.article.req.ArticleReq;
import com.eatme.web.common.vo.AppResponse;
import com.eatme.web.app.article.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/article/")
public class ArticleController {

    @Autowired
    ArticleService articleServiceImpl;

    @RequestMapping( "create")
    @ResponseBody
    public AppResponse create(@RequestBody com.eatme.web.app.article.entity.Article article){
        return  articleServiceImpl.addArticle(article);
    }
    @RequestMapping( "edit")
    @ResponseBody
    public AppResponse edit(@RequestBody com.eatme.web.app.article.entity.Article article){

        return  articleServiceImpl.editArticle(article);
    }
    @RequestMapping( "list")
    @ResponseBody
    //@RequiresRoles("admin")
    public AppResponse list(@PageableDefault(value = 10, sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, @RequestBody com.eatme.web.app.article.entity.Article article){
        return  articleServiceImpl.findList(article,PageRequest.of(0,10));
    }
    @RequestMapping( "detail/{id}")
    @ResponseBody
    public AppResponse detail(@PathVariable(value = "id") Integer id){

        return articleServiceImpl.getDetail(id);
    }
    @RequestMapping( "delete/{id}")
    @ResponseBody
    public AppResponse delete(@PathVariable(value = "id") Integer id){
        return  articleServiceImpl.delete(id);
    }

    @RequestMapping( "sort")
    @ResponseBody
    public AppResponse sort(@RequestBody ArticleReq article){
        return  articleServiceImpl.updateSort(article.getInitRanking(),article.getFinalRanking());
    }


    @RequestMapping("/query")
    @ResponseBody
    public AppResponse query(
            /*@PageableDefault(page = 0, value = 10, sort = {"createAt"}, direction = Sort.Direction.DESC) Pageable pageable*/
            @RequestBody com.eatme.web.app.article.entity.Article article) {
        Pageable pageable = Pageable.ofSize(10);
        return articleServiceImpl.query(article,pageable);
    }
}
